﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HiStaff.iSystem;
using HiStaff.Domain.HU;

namespace HiStaff.Control
{
    [ToolboxBitmap(typeof(TreeView))]
    public class HiTreeOrganization : TreeView
    {
        private bool _checkedAllChildNode = true;
        private static List<HU_ORGANIZATION> lstOrg;
        private static decimal? SelectedNoteID;
        private bool _searchControl = false;
        public bool SearchControl
        {
            get { return _searchControl; }
            set { _searchControl = value; }
        }
        public string DataField { get; set; }
        public decimal? OrganizationLevelID { get; set; }
        public decimal? OrganizationStatus { get; set; }
        public bool CheckedAllChildNode { get { return _checkedAllChildNode; } set { _checkedAllChildNode = value; } }
        public HiTreeOrganization()
        {
        }
        
        public void ReLoad()
        {
            try
            {
                TreeNode trNode;
                var _lst = Service.ProfileClient.GetOrganization();

                lstOrg = _lst.FindAll(obj => (!OrganizationLevelID.HasValue || obj.ORGLEVELID <= OrganizationLevelID) &&
                   (!OrganizationStatus.HasValue || obj.STATUS == OrganizationStatus));
                this.Nodes.Clear();
                if (lstOrg != null)
                {
                    //Populate base nodes
                    var query = (from c in lstOrg
                                 where !(from o in lstOrg
                                         select o.ID)
                                        .Contains(c.PARENTID)
                                 select c).ToList();

                    foreach (var item in query)
                    {
                        trNode = new TreeNode(item.NAME);
                        trNode.Name = item.ID.GetValueOrDefault(-1).ToString();
                        trNode.Tag = item.ORGLEVELID;
                        this.Nodes.Add(trNode);

                        PopulateTreeView(trNode);

                        trNode.Expand();
                    }

                    if (this.Nodes.Count > 0)
                    {
                        this.SelectedNode = this.Nodes[0];
                    }

                    //trvOrg.AfterCheck += new TreeViewEventHandler(trvOrg_AfterCheck);
                }
            }
            catch
            {
                return;
            }
        }
        private static string strNodes;
        public string GetCheckedNodes()
        {
            strNodes = ",";
            foreach (TreeNode child in this.Nodes)
            {
                CheckboxForChildNodes(child);
            }
            return strNodes;
        }
        public HU_ORGANIZATION GetDataRowSelectedNote()
        {
            if (this.SelectedNode != null)
            {
                var obj = (from o in lstOrg
                           where o.ID == SelectedNoteID
                           select o).FirstOrDefault();
                if (this.SelectedNode.Parent != null)
                {
                    obj.PARENTNAME = this.SelectedNode.Parent.Text;
                }
                return obj;
            }
            else return null;
        }

        private void CheckboxForChildNodes(TreeNode treeNode)
        {
            foreach (TreeNode node in treeNode.Nodes)
            {
                if (node.Checked)
                {
                    strNodes += node.Name + ",";
                }
                if (node.Nodes.Count > 0)
                {
                    // If the current node has child nodes, call the CheckAllChildsNodes method recursively.
                    this.CheckboxForChildNodes(node);
                }
            }
        }
        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            if (System.ComponentModel.LicenseManager.CurrentContext.UsageMode != System.ComponentModel.LicenseUsageMode.Designtime)
            {
                ReLoad();
            }
        }
        protected void PopulateTreeView(TreeNode trParentNode)
        {
            TreeNode trChildNode;
            var query = (from c in lstOrg
                         where c.PARENTID.Equals(decimal.Parse(trParentNode.Name))
                         select c).ToList();

            foreach (var item in query)
            {
                trChildNode = new TreeNode(item.NAME);
                trChildNode.Name = item.ID.GetValueOrDefault(-1).ToString();
                trChildNode.Tag = item.ORGLEVELID;
                trParentNode.Nodes.Add(trChildNode);

                PopulateTreeView(trChildNode);
            }
        }
        protected override void OnBeforeSelect(TreeViewCancelEventArgs e)
        {
            if (this.SelectedNode != null)
            {
                this.SelectedNode.BackColor = Color.Empty;
            }
            base.OnBeforeSelect(e);
        }
        protected override void OnAfterSelect(TreeViewEventArgs e)
        {
            try
            {
                GetFormParent(this).Cursor = Cursors.WaitCursor;
                e.Node.BackColor = Color.FromArgb(227, 239, 255);
                SelectedNoteID = decimal.Parse(e.Node.Name);
                base.OnAfterSelect(e);
            }
            catch
            {
            }
            finally
            {
                GetFormParent(this).Cursor = Cursors.Default;
            }
        }
        protected override void OnAfterCheck(TreeViewEventArgs e)
        {
            // The code only executes if the user caused the checked state to change.
            if (e.Action != TreeViewAction.Unknown)
            {
                if (_checkedAllChildNode)
                {
                    if (e.Node.Nodes.Count > 0)
                    {
                        /* Calls the CheckAllChildNodes method, passing in the current 
                        Checked value of the TreeNode whose checked state changed. */
                        this.CheckAllChildNodes(e.Node, e.Node.Checked);
                    }
                }
            }
            base.OnAfterCheck(e);
        }
        private void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked)
        {
            foreach (TreeNode node in treeNode.Nodes)
            {
                node.Checked = nodeChecked;
                if (node.Nodes.Count > 0)
                {
                    // If the current node has child nodes, call the CheckAllChildsNodes method recursively.
                    this.CheckAllChildNodes(node, nodeChecked);
                }
            }
        }

        public decimal? OrgId { get { return SelectedNoteID; } }
        private System.Windows.Forms.Control GetFormParent(System.Windows.Forms.Control control)
        {
            if (control.Parent.GetType().Name == "Form" ||
                control.Parent.GetType().BaseType.Name == "Form" ||
                control.Parent.GetType().BaseType.BaseType.Name == "Form" ||
                control.Parent.GetType().BaseType.BaseType.BaseType.Name == "Form" ||
                control.Parent.GetType().BaseType.BaseType.BaseType.BaseType.Name == "Form")
            {
                return control.Parent;
            }
            else
            {
                return GetFormParent(control.Parent);
            }
        }
    }
}
